<template>
	<view>
		<view class="tab3Style">
			<view class="row">
				<text>打乱模式</text>
				<uni-data-select :localdata="range" v-model="value" class="select" :clear="false"></uni-data-select>
			</view>
			<view class="row">
				<button type="default" size="mini" @click="clear">初始化限制条件</button>
			</view>
			
		</view>
		<view class="tab3Style">
			<view class="row">
				<text>角缓冲:</text>
				<text>{{bufferCorner[bufferIndex_C]}}</text>
			</view>
			
			<view class="row">
				<text>棱缓冲:</text>
				<text>{{bufferEdge[bufferIndex_E]}}</text>
			</view>
		
		</view>
		<view v-if="value===0">
			<!-- 三盲模式 -->
			
			
			<view class="tab3Style">
				<view class="row">
					<text>编码长度</text>
					<input v-model="cCount" @input="input"/>
				</view>
				<view class="row">
					<text>编码长度</text>
					<input v-model="eCount" @input="input"/>
				</view>
				
			</view>
			
			<view class="tab3Style">
				<view class="row">
					<text>翻色数量</text>
					<input v-model="cFsCount" @input="input"/>
				</view>
				<view class="row">
					<text>翻色数量</text>
					<input v-model="eFsCount" @input="input"/>
				</view>
				
			</view>
			
			<view class="tab3Style">
				<view class="row">
					<text>小循环数</text>
					<input v-model="cX" @input="input"/>
				</view>
				<view class="row">
					<text>小循环数</text>
					<input v-model="eX" @input="input"/>
				</view>
				
			</view>
			
			<view class="tab3Style">
				<view class="row">
					<text>缓冲归位</text>
					<uni-data-select :localdata="isHoming" v-model="cIndex" class="select" :clear="false"></uni-data-select>
				</view>
				<view class="row">
					<text>缓冲归位</text>
					<uni-data-select :localdata="isHoming" v-model="eIndex" class="select" :clear="false"></uni-data-select>
				</view>
				
			</view>
			
			<view class="column">
				<text>选择角块归位(可多选)</text>
				<uni-data-checkbox multiple v-model="cornerFix" :localdata="cFixArray" class="checkbox"></uni-data-checkbox>
			</view>
			<view class="column">
				<text>选择棱块归位(可多选)</text>
				<uni-data-checkbox multiple v-model="edgeFix" :localdata="eFixArray" class="checkbox"></uni-data-checkbox>
			</view>
			
			<view class="tab3Style">
				<view class="row">
					<text>奇偶校验</text>
					<uni-data-select :localdata="isParity" v-model="parity" class="select" :clear="false"></uni-data-select>
				</view>
				<view class="row" >
					<text>当前概率</text>
					<text>{{prob}}</text>
				</view>
				
			</view>
			
		</view>
		
		
		
		
		<view v-if="value===1">
			<!-- 反推模式 -->
			<view class="tab">
				<view class="row">
					<text>角块编码</text>
					<input v-model="cCode" @input="ft" style="-webkit-flex: 1;flex: 1;" id="c_code_id"/>
				</view>
			</view>
			
			<view class="tab">
				<view class="row">
					<text>角块翻色</text>
					<input v-model="cFs" @input="ft" style="-webkit-flex: 1;flex: 1;" id="c_fs_id"/>
				</view>
			</view>
			
			<view class="tab">
				<view class="row">
					<text>棱块编码</text>
					<input v-model="eCode" @input="ft" style="-webkit-flex: 1;flex: 1;" id="e_code_id"/>
				</view>
			</view>
			
			<view class="tab">
				<view class="row">
					<text>棱块翻色</text>
					<input v-model="eFs" @input="ft" style="-webkit-flex: 1;flex: 1;" id="e_fs_id"/>
				</view>
			</view>
			
		</view>
		
		
		<view class="tab3Style">
			<view class="row">
				<text>打乱坐标</text>
				<uni-data-select :localdata="isDisrupt" v-model="disrupt" class="select" :clear="false"></uni-data-select>
			</view>
			<view class="row">
				<button type="primary" size="mini" @click="create">生成打乱公式</button>
			</view>
			
		</view>
		
		<text class="result">{{scramble}}</text>
		<!-- <textarea v-model="scramble"   class="result"  /> -->
		<button v-if="scramble!=''"  type="default" :disabled="scramble==''" @click="copy">复制打乱公式</button>
		
	</view>
</template>

<script>
	import constant from '../../utils/constant';
	import cube from '../../utils/cube';
	import bldhelper from '../../utils/bldhelper.js'
	export default {
		data() {
			return {
				value:0,//打乱模式
				prob:"100%",
				range: [
				        {value: 0, text: "三盲模式"},
				        {value: 1, text: "反推模式"}
				      ],
				cIndex:7,//角缓冲归位情况
				eIndex:7,//棱缓冲归位情况
				isHoming:[
					{value: 7, text: "任意"},
					{value: 3, text: "归位色向任意"},
					{value: 1, text: "归位色向正确"},
					{value: 2, text: "归位色向错误"},
					{value: 4, text: "不归位"},
				],
				parity:3,//有无奇偶
				isParity:[
					{value: 3, text: "任意"},
					{value: 2, text: "有奇偶"},
					{value: 1, text: "无奇偶"}
				],
				disrupt:0,//是否打乱坐标
				isDisrupt:[
					{value: 0, text: "任意"},
					{value: 1, text: "打乱坐标"},
					{value: 2, text: "不打乱坐标"}
				],
				cornerFix:[],
				cFixArray:[],
				edgeFix:[],
				eFixArray:[],
				bufferCorner:["UBR","UFR","UFL","UBL","DBR","DFR","DFL","DBL"],
				bufferEdge:["UB","UR","UF","UL","UU",
				"BR","RR","FR","FF","FL","LL","BL","BB",
				"DB","DR","DF","DL","DD"],
				bufferIndex_C:1,
				bufferIndex_E:2,
				cCount:"0-10",//角编码长度
				eCount:"0-16",//棱编码长度
				cX:"0-3",//角小循环数
				eX:"0-5",//棱小循环数
				cFsCount:"0-7",//角翻色数量
				eFsCount:"0-11",//棱翻色数量
				scramble:"",//输出的打乱公式
				cCode:"",//角编码
				eCode:"",//棱编码
				cFs:"",//角翻色
				eFs:"",//棱翻色
				cornerS:[],
				edgeS:[]
			};
		},
		onLoad() {
			this.update();
			uni.$on('update',() =>{
				this.update();
			});
			
		},
		methods:{
			ft(event){
				var value = event.target.value;
				value = value.toUpperCase();
				var id = event.target.id;
				this.setcode(id,value);
			},
			input(event){
				
			},
			setcode(id,value){
				if(id=="c_code_id"){
					this.cCode=value;
				}else if(id=="c_fs_id"){
					this.cFs = value;
				}else if(id=="e_code_id"){
					this.eCode = value;
				}else if(id=="e_fs_id"){
					this.eFs = value;
				}
			},
			create(){
				if(this.value==1){//反推模式
					var cCode = this.cCode.replace(/\s+/g, "");
					var cFs = this.cFs.replace(/\s+/g, "");
					var eCode = this.eCode.replace(/\s+/g, "");
					var eFs = this.eFs.replace(/\s+/g, "");
					
					if(cCode.length%2!=eCode.length%2){
						uni.showToast({
							title:"奇偶错误",
							icon:'error'
						})
						return;
					}
					
					if(cFs.length>14||eFs.length>22||cFs.length%2!=0||eFs.length%2!=0){
						uni.showToast({
							title:"翻色错误",
							icon:'error'
						})
						return;
					}
					if(cFs.length==0&&eFs.length==0&&cCode.length==0&&eCode.length==0){
						//清空条件
						this.scramble="";
						return;
					}
					var cornerS = Array.from(this.cornerS);
					var edgeS = Array.from(this.edgeS);
					var res = cube.getScrambleFromCode(cornerS,edgeS,this.bufferIndex_C,this.bufferIndex_E,cCode,cFs,eCode,eFs,this.disrupt);
					var flag = res.flag;
					if(flag){
						uni.showToast({
							title:res.title,
							icon:'error'
						})
						return;
					}
					
					this.scramble = res.scramble;
					
				}else{
					
					var bufferC = this.getCornerBuffer();
					var cbuff = [bufferC,this.cIndex];
					var cnerrLR = this.cFsCount.split(/\-/g).map(str => Number(str));
					var cscycLR = this.cX.split(/\-/g).map(str => Number(str));
					var cncodeLR = this.cCount.split(/\-/g).map(str => Number(str));
					
					var buffE = this.getEdgeBuffer();
					var ebuff = [buffE,this.eIndex];
					var enerrLR =  this.eFsCount.split(/\-/g).map(str => Number(str));
					var escycLR = this.eX.split(/\-/g).map(str => Number(str));
					var encodeLR = this.eCount.split(/\-/g).map(str => Number(str));
					
					var ceparity = this.parity;
					var ceori = true;
					if(this.disrupt==0){
						var rot = Math.floor(Math.random() * 24);
						if(rot==1){
							ceori=false;
						}
					}else if(this.disrupt==1){
						ceori = true;
					}else{
						ceori = false;
					}
					var cfix = "";
					
					if(this.cornerFix.length>0){
						for(var i =0;i<this.cornerFix.length;i++){
							var index = this.cornerFix[i];
							for(var j=0;j<this.cFixArray.length;j++){
								var value = this.cFixArray[j].value;
								var text = this.cFixArray[j].text;
								if(index==value){
									cfix += text+" ";
									break;
								}
							}
						}
					}
					var efix = "";
					if(this.edgeFix.length>0){
						for(var i =0;i<this.edgeFix.length;i++){
							var index = this.edgeFix[i];
							for(var j=0;j<this.eFixArray.length;j++){
								var value = this.eFixArray[j].value;
								var text = this.eFixArray[j].text;
								if(index==value){
									efix += text+" ";
									break;
								}
							}
						}
					}
					
					
					var bldSets = {
						'cbuff': cbuff,//角缓冲，数组1位置1234567分别为 ok ,flip, ok/flip, move, ok/move,not ok, any
						'cfix': cfix,//归位角 除缓冲之外的角位置 字符串传入，例如 UBR DFL用空格分开
						'cnerrLR': cnerrLR,//角翻色
						'cscycLR': cscycLR,//角小循环
						'cncodeLR': cncodeLR,//角编码长度
						'ebuff': ebuff,//棱缓冲
						'efix': efix,//归位棱
						'enerrLR': enerrLR,//棱翻色
						'escycLR': escycLR,//棱小循环
						'encodeLR': encodeLR,//棱编码长度
						'ceparity': ceparity,//奇偶  1无奇偶 2有奇偶 3任意
						'ceori': ceori,//打乱坐标
					};
					
					var s = bldhelper.getScrambleWithSets(bldSets);
					this.prob = s[0];
					this.scramble = s[1];
					console.log(this.scramble);
				}
			},
			clear(){
				this.cIndex=7;//角缓冲归位
				this.eIndex=7;//棱缓冲归位
				this.parity=3;//有无奇偶
				this.disrupt=0;//是否打乱坐标
				this.cCount="0-10";//角编码长度
				this.eCount="0-16";//棱编码长度
				this.cX="0-3";//角小循环数
				this.eX="0-5";//棱小循环数
				this.cFsCount="0-7";//角翻色数量
				this.eFsCount="0-11";//棱翻色数量
				this.scramble="";//输出的打乱公式
				this.cCode="";//角编码
				this.eCode="";//棱编码
				this.cFs="";//角翻色
				this.eFs="";//棱翻色
				this.prob = "100%";
				this.cornerFix=[];
				this.edgeFix=[];
			},
			getCornerBuffer(){
				var buffer = this.bufferIndex_C;
				switch(buffer){
					case 0:return 3;
					case 1:return 0;
					case 2:return 1;
					case 3:return 2;
					case 4:return 7;
					case 5:return 4;
					case 6:return 5;
					case 7:return 6;
				}
			},
			getEdgeBuffer(){
				var buffer = this.bufferIndex_E
				switch(buffer){
					case 0:return 3;
					case 1:return 0;
					case 2:return 1;
					case 3:return 2;
					case 5:return 11;
					case 7:return 8;
					case 9:return 9;
					case 11:return 10;
					case 13:return 7;
					case 14:return 4;
					case 15:return 5;
					case 16:return 6;
				}
			},
			update(){
				this.bufferIndex_C = constant.getBufferIndex_C();
				this.bufferIndex_E = constant.getBufferIndex_E();
				this.cornerS = constant.getCornerS();
				this.edgeS = constant.getEdgeS();
				this.cFixArray = [
					{value: 0,text: "UBR","disable":this.bufferIndex_C==0},
					{value: 1,text: "UFR","disable":this.bufferIndex_C==1},
					{value: 2,text: "UFL","disable":this.bufferIndex_C==2},
					{value: 3,text: "UBL","disable":this.bufferIndex_C==3},
					{value: 4,text: "DBR","disable":this.bufferIndex_C==4},
					{value: 5,text: "DFR","disable":this.bufferIndex_C==5},
					{value: 6,text: "DFL","disable":this.bufferIndex_C==6},
					{value: 7,text: "DBL","disable":this.bufferIndex_C==7},
				];
				this.eFixArray=[
					{value: 0,text: "UB","disable":this.bufferIndex_E==0},
					{value: 1,text: "UR","disable":this.bufferIndex_E==1},
					{value: 2,text: "UF","disable":this.bufferIndex_E==2},
					{value: 3,text: "UL","disable":this.bufferIndex_E==3},
					{value: 5,text: "BR","disable":this.bufferIndex_E==5},
					{value: 7,text: "FR","disable":this.bufferIndex_E==7},
					{value: 9,text: "FL","disable":this.bufferIndex_E==9},
					{value: 11,text: "BL","disable":this.bufferIndex_E==11},
					{value: 13,text: "DB","disable":this.bufferIndex_E==13},
					{value: 14,text: "DR","disable":this.bufferIndex_E==14},
					{value: 15,text: "DF","disable":this.bufferIndex_E==15},
					{value: 16,text: "DL","disable":this.bufferIndex_E==16}
				];
				
			},
			copy(){
				
				uni.setClipboardData({
					data: this.scramble,
					success: function () {
						uni.showToast({
							title:'复制成功',
							duration:500
						})
					}
				});
			}
		}
	}
</script>

<style lang="scss">
.tab3Style{
	display: flex;
	flex-direction: row;
	align-items: center;
	padding: 10rpx;
	-webkit-justify-content: space-between;
	justify-content: space-between;
	
	input{
		width: 180rpx;
		border: solid;
		border-width: 1rpx;
		border-color: gray;
		border-radius: 5rpx;
		text-align: center;
	}
	
}

.tab{
	display: flex;
	flex-direction: row;
	align-items: center;
	padding: 10rpx;
	input{
		background-color: gainsboro;
		border: solid;
		border-width: 1rpx;
		border-color: gray;
		border-radius: 5rpx;
	}
}
.row{
	padding: 2rpx;
	display: flex;
	flex-direction: row;
	justify-content: center;
	align-items: center;
	-webkit-flex: 1;
	flex: 1;
	input{
		-webkit-flex: 1;
		flex: 1;
	}
	
}
.column{
	padding: 2rpx;
	display: flex;
	flex-direction: column;
	text{
		padding-left: 10rpx;
	}
}
.checkbox{
	padding-left: 12rpx;
}
.select{
	display: flex;
	-webkit-flex: 1;
	flex:1;
}
.result{
	font-size: 36rpx;
	display: flex;
	flex-direction: row;
	width: 750rpx;
	background-color: ghostwhite;
	
}
</style>
